import { request } from '@/utils/request'
import type { ApiResponse, PaginationParams, PaginationResponse } from '@/types/api'
import type {
  Project,
  ProjectCreateRequest,
  ProjectUpdateRequest,
  ProjectStatistics,
  Task,
  TaskCreateRequest,
  TaskUpdateRequest,
  TaskStatistics,
  Announcement,
  AnnouncementCreateRequest,
  AnnouncementUpdateRequest,
  AnnouncementStatistics
} from '@/types/project'

/**
 * 项目管理API
 */
export const projectApi = {
  /**
   * 创建项目
   */
  createProject(data: ProjectCreateRequest): Promise<ApiResponse<Project>> {
    return request.post('/projects', data)
  },

  /**
   * 更新项目
   */
  updateProject(id: number, data: ProjectUpdateRequest): Promise<ApiResponse<Project>> {
    return request.put(`/projects/${id}`, data)
  },

  /**
   * 删除项目
   */
  deleteProject(id: number): Promise<ApiResponse<void>> {
    return request.delete(`/projects/${id}`)
  },

  /**
   * 获取项目详情
   */
  getProject(id: number): Promise<ApiResponse<Project>> {
    return request.get(`/projects/${id}`)
  },

  /**
   * 获取团队项目列表
   */
  getTeamProjects(teamId: number, status?: string): Promise<ApiResponse<Project[]>> {
    const params = status ? { status } : {}
    return request.get(`/projects/team/${teamId}`, { params })
  },

  /**
   * 分页获取团队项目列表
   */
  getTeamProjectsPage(
    teamId: number,
    pagination: PaginationParams,
    status?: string
  ): Promise<ApiResponse<PaginationResponse<Project>>> {
    const params = {
      page: pagination.page,
      size: pagination.size,
      ...(status && { status })
    }
    return request.get(`/projects/team/${teamId}/page`, { params })
  },

  /**
   * 搜索项目
   */
  searchProjects(teamId: number, keyword: string): Promise<ApiResponse<Project[]>> {
    return request.get(`/projects/team/${teamId}/search`, {
      params: { keyword }
    })
  },

  /**
   * 更新项目状态
   */
  updateProjectStatus(id: number, status: string): Promise<ApiResponse<void>> {
    return request.put(`/projects/${id}/status`, null, {
      params: { status }
    })
  },

  /**
   * 分配项目经理
   */
  assignProjectManager(id: number, projectManagerId: number): Promise<ApiResponse<void>> {
    return request.put(`/projects/${id}/manager`, null, {
      params: { projectManagerId }
    })
  },

  /**
   * 更新项目进度
   */
  updateProjectProgress(id: number, progress: number): Promise<ApiResponse<void>> {
    return request.put(`/projects/${id}/progress`, null, {
      params: { progress }
    })
  },

  /**
   * 获取项目统计信息
   */
  getProjectStatistics(teamId: number): Promise<ApiResponse<ProjectStatistics>> {
    return request.get(`/projects/team/${teamId}/statistics`)
  }
}

/**
 * 项目任务API
 */
export const taskApi = {
  /**
   * 创建任务
   */
  createTask(data: TaskCreateRequest): Promise<ApiResponse<Task>> {
    return request.post('/tasks', data)
  },

  /**
   * 更新任务
   */
  updateTask(id: number, data: TaskUpdateRequest): Promise<ApiResponse<Task>> {
    return request.put(`/tasks/${id}`, data)
  },

  /**
   * 删除任务
   */
  deleteTask(id: number): Promise<ApiResponse<void>> {
    return request.delete(`/tasks/${id}`)
  },

  /**
   * 获取任务详情
   */
  getTask(id: number): Promise<ApiResponse<Task>> {
    return request.get(`/tasks/${id}`)
  },

  /**
   * 获取项目任务列表
   */
  getProjectTasks(projectId: number, status?: string): Promise<ApiResponse<Task[]>> {
    const params = status ? { status } : {}
    return request.get(`/tasks/project/${projectId}`, { params })
  },

  /**
   * 分页获取项目任务列表
   */
  getProjectTasksPage(
    projectId: number,
    pagination: PaginationParams,
    status?: string
  ): Promise<ApiResponse<PaginationResponse<Task>>> {
    const params = {
      page: pagination.page,
      size: pagination.size,
      ...(status && { status })
    }
    return request.get(`/tasks/project/${projectId}/page`, { params })
  },

  /**
   * 获取用户任务列表
   */
  getUserTasks(userId: number, status?: string): Promise<ApiResponse<Task[]>> {
    const params = status ? { status } : {}
    return request.get(`/tasks/user/${userId}`, { params })
  },

  /**
   * 搜索任务
   */
  searchTasks(projectId: number, keyword: string): Promise<ApiResponse<Task[]>> {
    return request.get(`/tasks/project/${projectId}/search`, {
      params: { keyword }
    })
  },

  /**
   * 更新任务状态
   */
  updateTaskStatus(id: number, status: string): Promise<ApiResponse<void>> {
    return request.put(`/tasks/${id}/status`, null, {
      params: { status }
    })
  },

  /**
   * 分配任务
   */
  assignTask(id: number, assigneeId: number): Promise<ApiResponse<void>> {
    return request.put(`/tasks/${id}/assign`, null, {
      params: { assigneeId }
    })
  },

  /**
   * 获取任务统计信息
   */
  getTaskStatistics(projectId: number): Promise<ApiResponse<TaskStatistics>> {
    return request.get(`/tasks/project/${projectId}/statistics`)
  },

  /**
   * 获取用户任务统计信息
   */
  getUserTaskStatistics(userId: number): Promise<ApiResponse<TaskStatistics>> {
    return request.get(`/tasks/user/${userId}/statistics`)
  }
}

/**
 * 项目公告API
 */
export const announcementApi = {
  /**
   * 创建公告
   */
  createAnnouncement(data: AnnouncementCreateRequest): Promise<ApiResponse<Announcement>> {
    return request.post('/announcements', data)
  },

  /**
   * 更新公告
   */
  updateAnnouncement(id: number, data: AnnouncementUpdateRequest): Promise<ApiResponse<Announcement>> {
    return request.put(`/announcements/${id}`, data)
  },

  /**
   * 删除公告
   */
  deleteAnnouncement(id: number): Promise<ApiResponse<void>> {
    return request.delete(`/announcements/${id}`)
  },

  /**
   * 获取公告详情
   */
  getAnnouncement(id: number): Promise<ApiResponse<Announcement>> {
    return request.get(`/announcements/${id}`)
  },

  /**
   * 获取项目公告列表
   */
  getProjectAnnouncements(projectId: number, pinned?: boolean): Promise<ApiResponse<Announcement[]>> {
    const params = pinned !== undefined ? { pinned } : {}
    return request.get(`/announcements/project/${projectId}`, { params })
  },

  /**
   * 分页获取项目公告列表
   */
  getProjectAnnouncementsPage(
    projectId: number,
    pagination: PaginationParams,
    excludePinned?: boolean
  ): Promise<ApiResponse<PaginationResponse<Announcement>>> {
    const params = {
      page: pagination.page,
      size: pagination.size,
      ...(excludePinned && { excludePinned })
    }
    return request.get(`/announcements/project/${projectId}/page`, { params })
  },

  /**
   * 搜索公告
   */
  searchAnnouncements(projectId: number, keyword: string): Promise<ApiResponse<Announcement[]>> {
    return request.get(`/announcements/project/${projectId}/search`, {
      params: { keyword }
    })
  },

  /**
   * 根据日期范围查询公告
   */
  getAnnouncementsByDateRange(
    projectId: number,
    startDate: string,
    endDate: string
  ): Promise<ApiResponse<Announcement[]>> {
    return request.get(`/announcements/project/${projectId}/date-range`, {
      params: { startDate, endDate }
    })
  },

  /**
   * 获取今日公告
   */
  getTodayAnnouncements(projectId: number): Promise<ApiResponse<Announcement[]>> {
    return request.get(`/announcements/project/${projectId}/today`)
  },

  /**
   * 获取本周公告
   */
  getWeekAnnouncements(projectId: number): Promise<ApiResponse<Announcement[]>> {
    return request.get(`/announcements/project/${projectId}/week`)
  },

  /**
   * 获取本月公告
   */
  getMonthAnnouncements(projectId: number): Promise<ApiResponse<Announcement[]>> {
    return request.get(`/announcements/project/${projectId}/month`)
  },

  /**
   * 置顶公告
   */
  pinAnnouncement(id: number): Promise<ApiResponse<void>> {
    return request.put(`/announcements/${id}/pin`)
  },

  /**
   * 取消置顶公告
   */
  unpinAnnouncement(id: number): Promise<ApiResponse<void>> {
    return request.put(`/announcements/${id}/unpin`)
  },

  /**
   * 获取公告统计信息
   */
  getAnnouncementStatistics(projectId: number): Promise<ApiResponse<AnnouncementStatistics>> {
    return request.get(`/announcements/project/${projectId}/statistics`)
  }
}
